#include "gui/SubWindow/ViewModifyComponentInfoWin.h"
#include <iostream>
#include "gtkmm/stock.h"

ViewModifyComponentInfoWin::ViewModifyComponentInfoWin(Component* comp) :
	m_mainFrame("Propiedades"), m_componentNameLabel("Nombre del componente"),
	m_typeLabel("Tipo del componente"),
	m_cardi("Cardinalidad"),
	m_radioButton1(m_radio,"0..1"),
	m_radioButton2(m_radio,"0..N"),
	m_radioButton3(m_radio,"1..1"),
	m_radioButton4(m_radio,"1..N"),
	m_radioButton5(m_radio,"N..N")

{
	std::string aux2,name;
	aux2= typeid((*comp)).name();

	m_component = comp;

	set_title("Informacion del componente");
	get_vbox()->pack_start(m_mainFrame, Gtk::PACK_EXPAND_WIDGET, 5);

	m_mainFrame.add(m_mainbox);

	m_mainbox.pack_start(m_componentNameHbox, Gtk::PACK_EXPAND_PADDING, 10);
	m_mainbox.pack_start(m_typeHbox, Gtk::PACK_EXPAND_PADDING, 10);

	m_componentNameHbox.pack_start(m_componentNameLabel, Gtk::PACK_EXPAND_PADDING, 10);
	m_componentNameHbox.pack_start(m_componentNameEntry, Gtk::PACK_EXPAND_PADDING, 10);

	m_typeHbox.pack_start(m_typeLabel, Gtk::PACK_EXPAND_PADDING, 10);
	m_typeHbox.pack_start(m_typeEntry, Gtk::PACK_EXPAND_PADDING, 10);


	if ( aux2 == "3Via" ){
		if (m_component->verifyConnectEntityRelation() ){
			name = comp->getCardinality();
			if ( strcmp(name.c_str(),"0..1") == 0 ){m_radioButton1.set_active(); }
			if ( strcmp(name.c_str(),"0..N") == 0 ){m_radioButton2.set_active(); }
			if ( strcmp(name.c_str(),"1..1") == 0 ){m_radioButton3.set_active(); }
			if ( strcmp(name.c_str(),"1..N") == 0 ){m_radioButton4.set_active(); }
			if ( strcmp(name.c_str(),"N..N") == 0 ){m_radioButton5.set_active(); }

			m_mainbox.add(m_cardi);
			m_mainbox.add(m_radioButton1);
			m_mainbox.add(m_radioButton2);
			m_mainbox.add(m_radioButton3);
			m_mainbox.add(m_radioButton4);
			m_mainbox.add(m_radioButton5);
		}
	}

	std::stringstream aux;
	aux << m_component->getType();
	m_typeEntry.set_text(aux.str());
	m_componentNameEntry.set_text(m_component->getName());

	add_button( Gtk::Stock::OK, Gtk::RESPONSE_OK);
	add_button( Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
}

ViewModifyComponentInfoWin::~ViewModifyComponentInfoWin() {
}

int ViewModifyComponentInfoWin::run(){

	std::string aux2;
	aux2= typeid((*m_component)).name();

	show_all();
	int res = Gtk::Dialog::run();
	if ( res == Gtk::RESPONSE_OK){
		m_component->setName(m_componentNameEntry.get_text());
		std::stringstream aux;
		unsigned type;
		aux << m_typeEntry.get_text();
		aux >> type;
		m_component->setType(type);

		if ( aux2 == "3Via" ){
			if (m_component->verifyConnectEntityRelation() ){
				if ( m_radioButton1.get_active() ) {
					m_component->setCardinality(m_radioButton1.get_label());
				}
				if (m_radioButton2.get_active() ) {
					m_component->setCardinality(m_radioButton2.get_label());
				}
				if (m_radioButton3.get_active() ) {
					m_component->setCardinality(m_radioButton3.get_label());
				}
				if (m_radioButton4.get_active() ) {
					m_component->setCardinality(m_radioButton4.get_label());
				}
				if (m_radioButton5.get_active() ) {
					m_component->setCardinality(m_radioButton5.get_label());
				}
			}
		}
	}
	return res;
}
